from django.db import models

#测试用例步骤执行结果表
class ZtTestResult(models.Model):
    id = models.IntegerField(primary_key=True)
    stepResults = models.IntegerField()
    # 数据输出样式
    def __str__(self):
        return f"['step_results': {self.stepResults}]"

    class Meta:
        db_table = 'zt_testresult'

#公司信息表
class ZtCompany(models.Model):

    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=120)

    class Meta:
        db_table = 'zt_company'

#部门信息表
class ZtDept(models.Model):

    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=60)

    class Meta:
        db_table = 'zt_dept'

#人员信息表
class ZtUser(models.Model):
    id = models.IntegerField(primary_key=True)
    account = models.CharField(max_length=30, unique=True)
    realname = models.CharField(max_length=100)
    password = models.CharField(max_length=32)
    deleted = models.CharField(max_length=10)
    mobile = models.CharField(max_length=11)
    join = models.DateField()

    company = models.ForeignKey(ZtCompany, on_delete=models.PROTECT, db_column='company', null=True, blank=True)
    dept = models.ForeignKey(ZtDept, on_delete=models.PROTECT, db_column='dept', null=True, blank=True)

    USERNAME_FIELD = 'account'
    REQUIRED_FIELDS = []

    # 这里是关键，确保 is_anonymous 是一个属性，而不是方法
    is_anonymous = False

    # 如果你需要，可以重写 is_authenticated
    is_authenticated = True

    class Meta:
        db_table = 'zt_user'

#分组权限
class ZtGroup(models.Model):

    id = models.IntegerField(primary_key=True)
    role = models.CharField(max_length=30)

    class Meta:
        db_table = 'zt_group'

#用户分组
class ZtUserGroup(models.Model):

    account = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='account', to_field='account', null=True,
                                   blank=True)
    group = models.ForeignKey(ZtGroup, on_delete=models.PROTECT, db_column='group', null=True, blank=True)

    class Meta:
        db_table = 'zt_usergroup'

#项目表
class ZtProject(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=90)
    project = models.IntegerField()
    type = models.CharField(max_length=30)
    # PM = models.CharField(max_length=30)
    teamCount = models.IntegerField()
    budget = models.CharField(max_length=30)
    begin = models.DateField()
    end = models.DateField()
    status = models.CharField(max_length=10)
    desc = models.TextField()
    deleted = models.CharField(max_length=10)
    path = models.CharField(max_length=255)
    hasProduct = models.IntegerField()
    RD = models.CharField(max_length=30)
    PO = models.CharField(max_length=30)
    QD = models.CharField(max_length=30)

    # PM = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='PM', related_name='PM_realname')
    PM = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='PM', to_field='account', null=True, blank=True)
    # PM = models.OneToOneField(ZtUser, on_delete=models.PROTECT, db_column='PM',
    #                                related_name='PM_realname')

    class Meta:
        db_table = 'zt_project'

#产品表
class ZtProduct(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=110)
    program = models.IntegerField()
    PO = models.CharField(max_length=30)
    deleted = models.CharField(max_length=10)

    #  产品与项目产品关联表一对多关系
    # project = models.ForeignKey('ZtProjectProduct', on_delete=models.PROTECT)

    class Meta:
        db_table = 'zt_product'

#产品计划表
class ZtProductPlan(models.Model):
    id = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=90)
    deleted = models.CharField(max_length=10)
    parent = models.IntegerField()

    product = models.ForeignKey(ZtProduct, on_delete=models.PROTECT, db_column='product', null=True, blank=True,
                                related_name='productPlan_product')

    #  产品与项目产品关联表一对多关系
    # project = models.ForeignKey('ZtProjectProduct', on_delete=models.PROTECT)

    class Meta:
        db_table = 'zt_productplan'

#项目产品关联表
class ZtProjectProduct(models.Model):
    # id = models.IntegerField(primary_key=True)

    project = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='project', null=True, blank=True, related_name='projectProduct_project')
    product = models.ForeignKey(ZtProduct, on_delete=models.PROTECT, db_column='product', null=True, blank=True, related_name='projectProduct_product')

    class Meta:
        db_table = 'zt_projectproduct'

#项目、执行迭代对应的团队
class ZtTeam(models.Model):
    # id = models.IntegerField(primary_key=True)
    # account = models.CharField(max_length=30)
    account = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='account', to_field='account', null=True,
                                blank=True)
    root = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='root', null=True, blank=True, related_name='team_project')

    class Meta:
        db_table = 'zt_team'

#功能模块表
class ZtModule(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=60)
    path = models.CharField(max_length=255)
    parent = models.IntegerField()
    deleted = models.CharField(max_length=10)

    # 产品和功能模块也是一对多的关系
    root = models.ForeignKey(ZtProduct, on_delete=models.PROTECT, db_column='root', null=True, blank=True, related_name='modules')

    class Meta:
        db_table = 'zt_module'

# 研发需求表
class ZtStory(models.Model):
    id = models.IntegerField(primary_key=True)
    parent = models.IntegerField()
    status = models.CharField(max_length=100)
    addStage = models.CharField(max_length=255)
    stage = models.CharField(max_length=100)
    title = models.CharField(max_length=255)
    # module = models.IntegerField()
    deleted = models.CharField(max_length=10)
    # assignedTo = models.CharField(max_length=30)
    pri = models.IntegerField()
    category = models.CharField(max_length=30)
    begin = models.DateField()
    end = models.DateField()
    type = models.CharField(max_length=30)
    plan = models.IntegerField()
    openedBy = models.CharField(max_length=30)
    changedBy = models.CharField(max_length=30)
    reviewedBy = models.CharField(max_length=255)
    openedDate = models.DateTimeField()
    changedDate = models.DateTimeField()
    reviewedDate = models.DateTimeField()

    # 产品和功能模块也是一对多的关系
    product = models.ForeignKey(ZtProduct, on_delete=models.PROTECT, db_column='product', null=True, blank=True,
                             related_name='story_product')
    module = models.ForeignKey(ZtModule, on_delete=models.PROTECT, db_column='module', null=True, blank=True, related_name='story_module')
    assignedTo = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='assignedTo', to_field='account', null=True,
                                   blank=True)

    class Meta:
        db_table = 'zt_story'

#项目需求关联表
class ZtProjectStory(models.Model):

    project = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='project', null=True, blank=True, related_name='projectStory_project')
    story = models.ForeignKey(ZtStory, on_delete=models.PROTECT, db_column='story', null=True, blank=True, related_name='projectStory_story')

    class Meta:
        db_table = 'zt_projectstory'

# 任务表
class ZtTask(models.Model):
    id = models.IntegerField(primary_key=True)
    # story = models.IntegerField()
    name = models.CharField(max_length=255)
    status = models.CharField(max_length=100)
    deleted = models.CharField(max_length=10)
    openedBy = models.CharField(max_length=30)
    openedDate = models.DateTimeField()
    assignedDate = models.DateTimeField()
    estStarted = models.DateField()
    deadline = models.DateField()
    realStarted = models.DateTimeField()
    finishedDate = models.DateTimeField()
    estimate = models.IntegerField()
    consumed = models.IntegerField()
    left = models.IntegerField()
    pri = models.IntegerField()
    mode = models.CharField(max_length=10)
    difficulty = models.CharField(max_length=255)
    desc = models.TextField()
    parent = models.IntegerField()
    type = models.CharField(max_length=20)

    project = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='project', null=True, blank=True, related_name='task_project')
    story = models.ForeignKey(ZtStory, on_delete=models.PROTECT, db_column='story', null=True, blank=True, related_name='task_story')
    execution = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='execution', null=True, blank=True, related_name='task_execution')
    module = models.ForeignKey(ZtModule, on_delete=models.PROTECT, db_column='module', null=True, blank=True, related_name='task_module')
    assignedTo = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='assignedTo', to_field='account',null=True,blank=True)
    finishedBy = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='finishedBy', to_field='account',null=True,blank=True,related_name='finishedBy_user')

    class Meta:
        db_table = 'zt_task'

#任务对应团队表
class ZtTaskTeam(models.Model):
    # id = models.IntegerField(primary_keky=True)
    estimate = models.IntegerField()
    consumed = models.IntegerField()
    left = models.IntegerField()
    order = models.IntegerField()
    status = models.CharField(max_length=30)

    task = models.ForeignKey(ZtTask, on_delete=models.PROTECT, db_column='task', null=True, blank=True, related_name='taskteam_task')
    account = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='account', to_field='account',null=True,blank=True)

    class Meta:
        db_table = 'zt_taskteam'

#测试单数据
class ZtTestTask(models.Model):
    id = models.IntegerField(primary_key=True)
    deleted = models.CharField(max_length=10)
    owner = models.CharField(max_length=30)
    members = models.TextField()
    pri = models.IntegerField()
    begin = models.DateField()
    end = models.DateField()
    desc = models.TextField()
    status = models.CharField(max_length=10)
    type = models.CharField(max_length=255)
    name = models.CharField(max_length=90)


    project = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='project', null=True, blank=True,
                                related_name='testTask_project')
    execution = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='execution', null=True, blank=True,
                                  related_name='testTask_execution')

    class Meta:
        db_table = 'zt_testtask'

#测试单数据
class ZtTestReport(models.Model):
    id = models.IntegerField(primary_key=True)
    deleted = models.CharField(max_length=10)
    title = models.CharField(max_length=255)
    tasks = models.CharField(max_length=255)
    owner = models.CharField(max_length=30)
    members = models.TextField()
    begin = models.DateField()
    end = models.DateField()
    report = models.TextField()

    project = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='project', null=True, blank=True,
                                related_name='testReport_project')
    execution = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='execution', null=True, blank=True,
                                  related_name='testReport_execution')

    class Meta:
        db_table = 'zt_testreport'

#用例表
class ZtCase(models.Model):
    id = models.IntegerField(primary_key=True)
    deleted = models.CharField(max_length=10)
    lastRunResult = models.CharField(max_length=30)
    title = models.CharField(max_length=255)
    pri = models.IntegerField()
    type = models.CharField(max_length=30)
    openedBy = models.CharField(max_length=30)
    lastRunner = models.CharField(max_length=30)

    product = models.ForeignKey(ZtProduct, on_delete=models.PROTECT, db_column='product', related_name='case_product')
    project = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='project', related_name='case_project')
    story = models.ForeignKey(ZtStory, on_delete=models.PROTECT, db_column='story', null=True, blank=True,
                              related_name='case_story')
    execution = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='execution', null=True, blank=True,
                                  related_name='case_execution')
    module = models.ForeignKey(ZtModule, on_delete=models.PROTECT, db_column='module', null=True, blank=True,
                               related_name='case_module')
    lastRunner = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='lastRunner', to_field='account',
                                   null=True, blank=True)

    class Meta:
        db_table = 'zt_case'

#测试单用例关联表
class ZtTestRun(models.Model):
    id = models.IntegerField(primary_key=True)
    lastRunResult = models.CharField(max_length=30)
    assignedTo = models.CharField(max_length=30)

    task = models.ForeignKey(ZtTestTask, on_delete=models.PROTECT, db_column='task', null=True, blank=True, related_name='testRun_task')
    case = models.ForeignKey(ZtCase, on_delete=models.PROTECT, db_column='case', null=True, blank=True, related_name='testRun_case')

    class Meta:
        db_table = 'zt_testrun'

#bug单
class ZtBug(models.Model):
    id = models.IntegerField(primary_key=True)
    project = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='project', related_name='bug_project')
    # story = models.IntegerField()
    status = models.CharField(max_length=100)
    deleted = models.CharField(max_length=10)
    deadline = models.DateField()
    title = models.CharField(max_length=255)
    keywords = models.CharField(max_length=255)
    severity = models.IntegerField()
    pri = models.IntegerField()
    assignedDate = models.DateTimeField()
    steps = models.TextField()
    confirmed = models.IntegerField()
    openedDate = models.DateTimeField()
    closedDate = models.DateTimeField()
    resolvedBy = models.CharField(max_length=30)
    openedBy = models.CharField(max_length=30)
    closedBy = models.CharField(max_length=30)

    product = models.ForeignKey(ZtProduct, on_delete=models.PROTECT, db_column='product', related_name='bug_product')
    story = models.ForeignKey(ZtStory, on_delete=models.PROTECT, db_column='story', null=True, blank=True, related_name='bug_story')
    execution = models.ForeignKey(ZtProject, on_delete=models.PROTECT, db_column='execution', null=True, blank=True,
                                  related_name='bug_execution')
    module = models.ForeignKey(ZtModule, on_delete=models.PROTECT, db_column='module', null=True, blank=True, related_name='bug_module')
    assignedTo = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='assignedTo', to_field='account',
                                   null=True, blank=True)

    class Meta:
        db_table = 'zt_bug'

#文件
class ZtFile(models.Model):
    id = models.IntegerField(primary_key=True)
    objectType = models.CharField(max_length=30)
    objectID = models.IntegerField()
    deleted = models.CharField(max_length=10)
    extra = models.CharField(max_length=255)
    title = models.CharField(max_length=255)

    class Meta:
        db_table = 'zt_file'

#操作日志
class ZtAction(models.Model):
    id = models.IntegerField(primary_key=True)
    objectType = models.CharField(max_length=30)
    objectID = models.IntegerField()
    action = models.CharField(max_length=80)
    date = models.DateTimeField()
    extra = models.TextField()

    actor = models.ForeignKey(ZtUser, on_delete=models.PROTECT, db_column='actor', to_field='account',
                                   null=True, blank=True)

    class Meta:
        db_table = 'zt_action'